package com.hundsun.documentripper.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hundsun.documentripper.domain.dto.DocumentVersionView;
import com.hundsun.documentripper.domain.pojo.DocumentVersion;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：文档版本Mapper
 * @date ：Created in 2021/1/27
 */
public interface DocumentVersionMapper extends BaseMapper<DocumentVersion> {

    @Select("select * from document_version a join\n" +
            "(select \n" +
            "v.document_uuid,\n" +
            "v.product_version_uuid,\n" +
            "max(v.document_version) as document_version\n" +
            "from document_version v\n" +
            "where v.product_version_uuid = #{productVersionUuid}\n" +
            "group by v.document_uuid, v.product_version_uuid) b\n" +
            "on a.document_uuid = b.document_uuid\n" +
            "and a.product_version_uuid = b.product_version_uuid\n" +
            "and a.document_version = b.document_version")
    List<DocumentVersion> getLastDocumentVersionByProductVersion(String productVersionUuid);


    @Select("select a.* , p.code as product_version_code, IFNULL(f.name, h.name) as file_name \n" +
            "from document_version a \n" +
            "left join product_version p on a.product_version_uuid = p.uuid \n" +
            "left join file f on a.file_uuid = f.uuid \n" +
            "left join file_history h on a.file_uuid = h.uuid \n" +
            "${ew.customSqlSegment}")
    IPage<DocumentVersionView> selectDocumentVersion(Page<DocumentVersionView> page, @Param(Constants.WRAPPER) Wrapper<DocumentVersionView> wrapper);

    @Select("select a.* , p.code as product_version_code, IFNULL(f.name, h.name) as file_name \n" +
            "from document_version a \n" +
            "left join product_version p on a.product_version_uuid = p.uuid \n" +
            "left join file f on a.file_uuid = f.uuid \n" +
            "left join file_history h on a.file_uuid = h.uuid \n" +
            "${ew.customSqlSegment}")
    List<DocumentVersionView> selectAllDocumentVersion(@Param(Constants.WRAPPER) Wrapper<DocumentVersionView> wrapper);
}